import { getUserAgreement, updateUserAgreement, getPrivacyAgreement, updatePrivacyAgreement, getAppList, deleteApp, shelves, updateApp } from '@/api/serviceManage'
const oldState = { // 用于重置state数据
  userAgreement: '',
  privacyAgreement: '',
  appList: []
}

const state = JSON.parse(JSON.stringify(oldState))

const mutations = {
  set_userAgreement (state, data) {
    state.userAgreement = data
  },
  set_privacyAgreement (state, data) {
    state.privacyAgreement = data
  },
  set_appList (state, data) {
    state.appList = data
  }
}
const actions = {
  getUserAgreementData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getUserAgreement(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_userAgreement', res.data.message)
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  updateUserAgreementData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      updateUserAgreement(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data)
        }
      })
    })
  },
  getPrivacyAgreementData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getPrivacyAgreement(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_privacyAgreement', res.data.message)
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  updatePrivacyAgreementData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      updatePrivacyAgreement(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data)
        }
      })
    })
  },
  getAppListData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getAppList(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_appList', res.data.data.list)
          // commit('set_total', res.data.data.size)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_appList', [])
          // commit('set_total', 0)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 删除应用
  deleteAppData ({ state, commit }, playload) {
    return new Promise((resolve, reject) => {
      deleteApp(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 上架应用
  shelvesData ({ state, commit }, playload) {
    return new Promise((resolve, reject) => {
      shelves(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 编辑应用
  updateAppData ({ state, commit }, playload) {
    return new Promise((resolve, reject) => {
      updateApp(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  }

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
